package com.tengju.support.domain.model.resource;

import com.tengju.support.domain.shared.DomainException;
import com.tengju.support.domain.shared.DomainExceptionCode;
import lombok.Value;
import lombok.experimental.NonFinal;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Value
public class Resource {

    /**
     * 资源id
     */
    ResourceId resourceId;

    /**
     * 跳转方式
     */
    JumpAction action;

    /**
     * 图片地址
     */
    @NonFinal
    String imgUrl;

    /**
     * 标题
     */
    @NonFinal
    String title;

    /**
     * 跳转url
     */
    @NonFinal
    String jumpUrl;

    /**
     * 创建人
     */
    String createBy;

    /**
     * 位置
     */
    @NonFinal
    Position position;

    /**
     * 开始时间
     */
    @NonFinal
    LocalDateTime beginTime;

    /**
     * 结束时间
     */
    @NonFinal
    LocalDateTime endTime;

    /**
     * 用户定向
     */
    @NonFinal
    List<ResourceOrientation> userOrientation;

    /**
     * 优先级
     */
    @NonFinal
    Integer priority;

    /**
     * 变更人
     */
    @NonFinal
    String updateBy;

    /**
     * 创建时间
     */
    @NonFinal
    LocalDateTime createTime;


    public void edit(String title,Integer position,String imgUrl,String jumpUrl,LocalDateTime beginTime,LocalDateTime endTime,List<Integer> userOrientation,Integer priority,String updateBy){

        if (!StringUtils.isEmpty(title)) {
            this.title = title;
        }
        if (Position.fromVal(position) != null) {
            this.position = Position.fromVal(position);
        }
        if (!StringUtils.isEmpty(imgUrl)) {
            this.imgUrl = imgUrl;
        }
        if (!StringUtils.isEmpty(jumpUrl)) {
            this.jumpUrl = jumpUrl;
        }
        if (beginTime != null) {
            this.beginTime = beginTime;
        }
        if (endTime != null) {
            this.endTime = endTime;
        }
        if (!CollectionUtils.isEmpty(userOrientation)) {
            this.userOrientation = userOrientation.stream().map(ResourceOrientation::fromVal).collect(Collectors.toList());
        }
        if (priority != null) {
            this.priority = priority;
        }
        if (StringUtils.isEmpty(updateBy)) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA,"变更人为空");
        }
        this.updateBy = updateBy;
    }

    public boolean isAllOrien() {
        return userOrientation.size() == 8;
    }

}
